package cn.iocoder.yudao.module.api.pay.order.dto;

import cn.iocoder.yudao.framework.validation.InEnumCode;
import cn.iocoder.yudao.module.api.pay.order.eums.UnifiedPayChannelEnum;
import cn.iocoder.yudao.module.api.pay.order.eums.UnifiedPayDisplayModeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.URL;

import java.time.LocalDateTime;
import java.util.Map;

import static cn.iocoder.yudao.module.api.pay.ConfigConstants.PAY_TIMEOUT_MINUTES;

/**
 * 支付单创建 DTO
 */
@Data
@Accessors(chain = true)
public class UnifiedOrderCreateDTO extends PayOrderCreateDTO {

    @Schema(description = "统一支付单号，每请求一次对应一个", example = "UPY202406271105343545")
    @NotBlank(message = "统一支付单号不能为空")
    private String payNo;

    @Schema(description = "支付结果的回调地址", example = "https://example.local/app-api/pay/order/notify")
    @URL(message = "支付结果的通知回调地址必须是 URL 格式")
    private String notifyUrl;

    @Schema(description = "支付结果的跳转回调地址，用于网页支付", example = "https://example.local/app-api/pay/order/redirect")
    @URL(message = "支付结果的跳转回调地址必须是 URL 格式")
    private String redirectUrl;

    @Schema(description = "支付渠道：alipay_app、wx_lite、wallet", example = "wallet")
    @InEnumCode(UnifiedPayChannelEnum.class)
    private String channelCode;

    @Schema(description = "支付渠道的额外参数，例如微信公众号需要传递 openid 参数")
    private Map<String, String> channelExtras;

    @Schema(description = "展示模式：none、url、iframe、vo、qr_code、qr_code_url、bar_code、app，参见 PayDisplayModeEnum 枚举类。如果不传递，则每个支付渠道使用默认的方式", example = "app")
    @InEnumCode(UnifiedPayDisplayModeEnum.class)
    private String displayMode = UnifiedPayDisplayModeEnum.NONE.getCode();

    @Schema(description = "支付过期时间")
    private LocalDateTime expireTime;

    public LocalDateTime getExpireTime() {
        if (expireTime == null) {
            expireTime = LocalDateTime.now().plusMinutes(PAY_TIMEOUT_MINUTES);
        }
        return expireTime;
    }

    @Schema(description = "用户 IP", example = "127.0.0.1")
    private String userIp = "";

    @Schema(description = "用户 UA")
    private String userUa;

}
